<?php

class ImageLib {

    private $path;

    public function __construct() {
        $this->path = PUBLIC_DIR . '/upload';
    }

    public function upload() {
        
    }

    public function multi_upload($data_file) {
        if (empty($data_file) == TRUE) {
            return FALSE;
        }

        for ($i = 0, $count = count($data_file['name']); $i < $count; $i++) {
            $filename = $data_file['name'][$i];
            $filetype = $data_file['type'][$i];
            if ($this->check_mime_type($filename, $filetype) == FALSE) {
                return FALSE;
            }
        }

        $fb_id = $_SESSION['user_info']['facebook_id'];

        $response = array();
        for ($i = 0, $count = count($data_file['name']); $i < $count; $i++) {
            $temp = explode(".", $filename);
            $extension = end($temp);

            if (is_dir($this->path . '/' . $fb_id) == FALSE) {
                @mkdir($this->path . '/' . $fb_id, 0777, TRUE);
            }

            $filename = md5($data_file['name'][$i] . mktime()) . '.' . $extension;
            
            $source = $data_file['tmp_name'][$i];
            $destination = $this->path . '/' . $fb_id . '/' . $filename;
            move_uploaded_file($source, $destination);
            $response[] = $filename;
        }

        return $response;
    }

    private function check_mime_type($filename, $filetype) {
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $mime_type = array('image/jpeg', 'image/jpg', 'image/pjpeg', 'image/x-png', 'image/png');
        $temp = explode(".", $filename);
        $extension = end($temp);


        if (in_array($filetype, $mime_type)
                AND in_array($extension, $allowedExts)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}
